package com.realgodo.site.web.space;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.realgodo.site.common.Constant;
import com.realgodo.site.entity.application.mood.Mood;
import com.realgodo.site.entity.dic.Face;
import com.realgodo.site.entity.space.Space;
import com.realgodo.site.service.UserManager;
import com.realgodo.site.service.application.MoodManager;
import com.realgodo.site.service.dic.FaceManager;
import com.realgodo.site.service.space.SpaceManager;
import com.realgodo.site.service.space.SpaceVisitManager;
import com.realgodo.site.web.ControllerSupport;

public class SpaceDefaultController extends ControllerSupport {

	private static final long serialVersionUID = 1462079600722317964L;

	private String domain;

	/** 被访问者Space */
	private Space vSpace;
	/** 当前登录者Space */
	private Space space;

	@Autowired
	private SpaceManager spaceManager;

	@Autowired
	protected MoodManager moodManager;
	@Autowired
	private UserManager userManager;
	@Autowired
	private FaceManager faceManager;
	/** 全部表情列表 */
	private List<Face> faceList;
	private List<Face> commenFaceList;

	private List<Face> planeFaceList;

	private List<Face> dimensionalFaceList;

	@Autowired
	private SpaceVisitManager spaceVisitManager;

	/**
	 * 创建访问记录
	 */
	protected void createSpaceVisit() {
		if (getSpace() != null && getVSpace() != null) {
			spaceVisitManager.createSpaceVisit(getVSpace().getId(), getSpace()
					.getId());
		}

	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	/** 当前登录者的space */
	public Space getSpace() {
		space = (space == null) ? (Space) getSession().getAttribute(
				Constant.LOGIN_SPACE) : space;
		return space;
	}

	/** 获得当前访问的space */
	public Space getVSpace() {
		if (domain != null) {
			vSpace = this.spaceManager.getSpaceByDomain(getDomain());
			super.getSession().setAttribute(Constant.VISIT_SPACE, vSpace);
		} else {
			vSpace = (Space) super.getSession().getAttribute(
					Constant.VISIT_SPACE);
		}
		if (vSpace != null) {
			vSpace.setUser(this.userManager.getUserForDisplay(vSpace.getUser()
					.getId()));
		}
		return vSpace;
	}

	private void spiltFaceList() {
		faceList = getFaceList();
		commenFaceList = new ArrayList<Face>();
		planeFaceList = new ArrayList<Face>();
		dimensionalFaceList = new ArrayList<Face>();
		for (Face face : faceList) {
			if (face.getType() == 1) {
				commenFaceList.add(face);
			} else if (face.getType() == 2) {
				planeFaceList.add(face);
			} else {
				dimensionalFaceList.add(face);
			}
		}
	}

	public String getNameSpace() {
		String uri = getPageUrl();
		if (uri != null && !"".equals(uri.trim())) {
			String[] spaces = uri.split("/");
			if (spaces[spaces.length - 1].indexOf('!') != -1) {
				nameSpace = spaces[spaces.length - 1].split("!")[0];
			} else if (spaces.length >= 2) {
				nameSpace = spaces[spaces.length - 2];
			}
		}
		return nameSpace;
	}

	// -----------get/set-----//
	public List<Face> getFaceList() {
		if (faceList == null) {
			faceList = faceManager.getAll();
		}
		return faceList;
	}

	public List<Face> getCommenFaceList() {
		if (commenFaceList == null) {
			spiltFaceList();
		}
		return commenFaceList;
	}

	public List<Face> getPlaneFaceList() {
		if (planeFaceList == null) {
			spiltFaceList();
		}
		return planeFaceList;
	}

	public List<Face> getDimensionalFaceList() {
		if (dimensionalFaceList == null) {
			spiltFaceList();
		}
		return dimensionalFaceList;
	}

	public String getShout() {
		Mood _last = moodManager.getLastMood(getVSpace().getId());
		return (_last != null) ? _last.getContent() : "";
	}
}
